package com.competition.reception.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.competition.commonutils.MD5;
import com.competition.commonutils.R;
import com.competition.reception.entity.Manage;
import com.competition.reception.mapper.ManageMapper;
import com.competition.reception.service.ManageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.competition.servicebase.exceptionhandler.MyException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.List;

/**
 * <p>
 * 管理员登录表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-04-03
 */
@Service
public class ManageServiceImpl extends ServiceImpl<ManageMapper, Manage> implements ManageService {

    //添加管理员
    @Override
    public void addManage(Manage manage) {
        QueryWrapper<Manage> wrapper=new QueryWrapper<>();
        //根据用户名查找管理员
        wrapper.eq("manage_email",manage.getManageUsername());
        Manage selectOne = baseMapper.selectOne(wrapper);
        //如果用户名已存在,不添加
        if(selectOne!=null) throw new MyException(201,"该管理员已存在");
        //添加管理员
        manage.setManagePassword(MD5.encrypt(manage.getManagePassword()));
        baseMapper.insert(manage);
    }
    //修改管理员资料
    @Override
    public void updateManage(Manage manage) {
        //判断管理员是否存在
        Manage manageOne = baseMapper.selectById(manage);
        //不存在
        if(manageOne==null) throw new MyException(201,"该管理员不存在");
        //提交修改资料
        int flag = baseMapper.updateById(manage);
        if(flag!=1)   throw new MyException(201,"修改失败");
    }
    //查询管理员并分页
    @Override
    public R selectManage(Integer page, Integer limit, String condition) {
        QueryWrapper<Manage> queryWrapper=new QueryWrapper<>();
        //根据条件有无查询
        if(!StringUtils.isEmpty(condition)){
            queryWrapper.like("manage_username",condition);
            queryWrapper.or();
            queryWrapper.eq("manage_email",condition);
            queryWrapper.orderByDesc("create_time");
        }
        //根据分页数,数量查询数据
        Page<Manage> pageManage=new Page<Manage>(page,limit);
        //返回分页数据
        IPage<Manage> manageIPage = baseMapper.selectPage(pageManage, queryWrapper);
        //查询数据总数
        long total = manageIPage.getTotal();
        //查询具体记录
        List<Manage> records = manageIPage.getRecords();
        //返回页数
        long current = manageIPage.getCurrent();
        return R.ok().data("total",total).data("current",current).data("records",records);
    }
    //删除管理员
    @Override
    public void removeManage(String id) {
        baseMapper.deleteById(id);
    }
    //根据id将管理员添加至黑名单或者取消黑名单
    @Override
    public Integer BlacklistManage(String id) {
        Manage manage = baseMapper.selectById(id);
        if(manage.getIsBlacklist()==1) {
            manage.setIsBlacklist(0);
        }else {
            manage.setIsBlacklist(1);
        }
        int i = baseMapper.updateById(manage);
        if(i!=1) throw new MyException(201,"修改失败");
        return manage.getIsBlacklist();
    }

}
